﻿/*
#Author: Qtjun
#WeChat Official Accounts: qthub_com
#QQ Group: 732271126
#Email: 2088201923@qq.com
*/

#include "NumberKeyboard.h"
#include "KeyButton.h"
#include <QVBoxLayout>

using namespace AeaQt;

typedef QList<KeyButton::Mode> Modes;
typedef QList<Modes> ModesList;

static const QString s_backspace_icon = ":/Image/backspace.png";
static const QString s_space_icon = ":/Image/space.png";
static const QString s_pack_up_icon = ":/Image/pack_up.png";

NumberKeyboard::NumberKeyboard(QWidget *parent) : AbstractKeyboard(parent)
{
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setSizeConstraint(QLayout::SetNoConstraint);
    layout->setSpacing(0);
    layout->setMargin(0);

    // 4行3列布局，只保留数字和退格
    QList<QList<KeyButton::Mode>> rows = {
        {{Qt::Key_1, "1"}, {Qt::Key_2, "2"}, {Qt::Key_3, "3"}},
        {{Qt::Key_4, "4"}, {Qt::Key_5, "5"}, {Qt::Key_6, "6"}},
        {{Qt::Key_7, "7"}, {Qt::Key_8, "8"}, {Qt::Key_9, "9"}},
        {{Qt::Key_Backspace, "", QIcon(s_backspace_icon)}, {Qt::Key_0, "0"}, {Qt::Key_unknown, ""}}};

    for (const auto &row : rows)
    {
        QHBoxLayout *h = new QHBoxLayout;
        h->setSizeConstraint(QLayout::SetNoConstraint);
        for (const auto &mode : row)
        {
            KeyButton *button = createButton({mode}); // 修正为列表
            h->addWidget(button);
        }
        layout->addLayout(h);
    }

    this->setLayout(layout);
}

KeyButton *NumberKeyboard::createButton(QList<KeyButton::Mode> modes)
{
    KeyButton *button = new KeyButton(modes, this);
    button->onReponse(this, SLOT(onButtonPressed(const int &, const QString &)));
    button->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    return button;
}

void NumberKeyboard::onButtonPressed(const int &code, const QString &text)
{
    onKeyPressed(code, text);
}
